import {
  CrumbItem,
  IOrgItem,
  IPositionDetail,
  IRoleDetail,
  TChoiceStaffValue,
  TPerson,
  TPositionGroupItem,
  TRoleGroupItem,
} from './interface';

//全选checkbox状态
export const getCheckboxStaObj = (
  crumbList: CrumbItem[],
  depNum: Record<string, { totalNum: number; hasSelNum: number }>,
) => {
  console.log('--checked--crumbList--depNum--:', crumbList, depNum);
  const tempStaObj = { indeterminate: false, checked: false };
  const currDepId = crumbList[crumbList.length - 1].guid;
  if (currDepId && Object.keys(depNum).length > 0) {
    const currDepHasSelNum = depNum[currDepId].hasSelNum;
    const currDepTotalNum = depNum[currDepId].totalNum;
    tempStaObj.indeterminate =
      currDepHasSelNum > 0 && currDepHasSelNum < currDepTotalNum ? true : false;
    tempStaObj.checked =
      currDepHasSelNum > 0 && currDepHasSelNum === currDepTotalNum
        ? true
        : false;
  }
  return tempStaObj;
};

export const getOrgCheckboxStaObj = (
  selectedValues: TChoiceStaffValue[],
  groupList: IOrgItem[],
  staffList: TPerson[],
  onlyChoicePeople: boolean,
) => {
  let totalNum: number = 0;
  let combineList: any[] = [];
  if (onlyChoicePeople) {
    combineList = staffList;
    totalNum = staffList.length;
  } else {
    combineList = [...groupList, ...staffList];
    totalNum = combineList.length;
  }
  const hasSelNum = combineList.reduce((prev: number, curr: any) => {
    const existIndex = selectedValues.findIndex(
      (selItem) => selItem.combineId === curr.combineId,
    );
    if (existIndex > -1) {
      prev += 1;
    }
    return prev;
  }, 0);
  const tempStaObj = { indeterminate: false, checked: false };
  if (hasSelNum > 0) {
    if (hasSelNum < totalNum) {
      tempStaObj.indeterminate = true;
    } else {
      tempStaObj.checked = true;
    }
  }

  return tempStaObj;
};

//全选checkbox状态
export const getOtherCheckboxStaObj = (
  type: 'role' | 'pos',
  selectedValues: TChoiceStaffValue[],
  groupList:
    | IRoleDetail['roleGroupList']
    | IPositionDetail['positionGroupList'],
  staffList: TPerson[],
  onlyChoicePeople: boolean,
) => {
  let totalNum: number = 0;
  let combineList: any[] = [];
  if (onlyChoicePeople) {
    combineList = staffList;
    totalNum = staffList.length;
  } else {
    groupList?.forEach((groupItem) => {
      const childList =
        type === 'role'
          ? (groupItem as TRoleGroupItem).rlist || []
          : (groupItem as TPositionGroupItem).plist || [];
      combineList.push(...childList);
    });
    combineList.push(...staffList);
    totalNum = combineList.length;
  }

  const hasSelNum = combineList.reduce((prev: number, curr: any) => {
    const existIndex = selectedValues.findIndex(
      (selItem) => selItem.combineId === curr.combineId,
    );
    if (existIndex > -1) {
      prev += 1;
    }
    return prev;
  }, 0);
  const tempStaObj = { indeterminate: false, checked: false };
  if (hasSelNum > 0) {
    if (hasSelNum < totalNum) {
      tempStaObj.indeterminate = true;
    } else {
      tempStaObj.checked = true;
    }
  }

  return tempStaObj;
};
